<template>
  <div class="app-container">
    <div class="container">
      <el-alert title="菜单信息修改完成后需退出重新登录才能生效！" type="success" show-icon />
      <el-table ref="tableData" :data="tableData" border row-key="id" :default-expand-all="false" highlight-current-row :tree-props="{ children: 'children', hasChildren: false }">
        <el-table-column type="index" label="索引" width="50px" align="center" />
        <el-table-column prop="name" label="菜单名称" align="left" />
        <el-table-column prop="sort" label="菜单排序" align="center" width="150px" />
        <el-table-column prop="status" label="是否展示" width="150px" align="center">
          <template #default="{ row }">
            <el-switch v-model="row.status" v-loading="loading1" :disabled="['A', 'A-5'].includes(row.code) || (row.children && row.children.some(el => el.status === 1))" :active-value="1" :inactive-value="0" @change="submitApi(row)" />
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="菜单描述" align="center" />
        <el-table-column label="操作" align="center" width="100px">
          <template #default="{ row }">
            <el-button type="text" icon="el-icon-edit" @click="showEdit(row)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="编辑" :visible.sync="dialogVisible" width="350px">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px" :inline="false">
          <el-form-item label="菜单名称" prop="name">
            <el-input v-model.trim="ruleForm.name" />
          </el-form-item>
          <el-form-item label="菜单排序" prop="sort">
            <el-input-number v-model="ruleForm.sort" style="width: 100%" :max="999" :precision="0" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="demo-drawer__footer">
          <el-button size="medium" @click="dialogVisible = false">取 消</el-button>
          <el-button size="medium" type="primary" :loading="loading1" @click="sbumitForm('ruleForm')">{{ loading1 ? '提交中 ...' : '提 交' }}</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { treeData, getTreeMap } from '@/utils/function'
export default {
  components: {},
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      loading: false,
      loading1: false,
      ruleForm: {},
      rules: {
        name: [{ required: true, message: '菜单名称不能为空！' }],
        sort: [{ required: true, message: '排序不能为空！' }]
      }
    }
  },
  computed: {
    ...mapGetters(['compInfo'])
  },
  created() {},
  mounted() {
    this.getTableInfo()
  },
  methods: {
    async getTableInfo() {
      this.loading = true
      const res = await this.$axios.get(
        `/platform/menu/listComp?compId=${this.compInfo.compId}`
      )

      if (res?.data.code === 200) {
        const list = getTreeMap(res.data.data).filter((item) => {
          delete item.children
          delete item.parentNode
          return item.allocated === 1
        })

        this.tableData = treeData(list, 'id', 'parentId', 'children')
        this.loading = false
      }
    },

    /**
     * @description: data modify
     * @param {*} data
     * @return {*}
     */
    showEdit(data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.ruleForm = Object.assign({}, data)
      })
    },
    /**
     * @description: sbumit from
     * @return {*}
     */
    sbumitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading1 = true
          this.submitApi()
        }
      })
    },
    submitApi(row) {
      this.$axios.post('/comp/menu/update', row || this.ruleForm).then((res) => {
        if (res?.data.code === 200 && res.data.data) {
          this.$notify.success('修改成功！')
          this.loading1 = false
          this.dialogVisible = false
          this.getTableInfo()
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.container {
  .el-alert {
    margin-bottom: 10px;
  }
  .demo-drawer__footer {
    display: flex;
    .el-button {
      flex: 1;
    }
  }
}
</style>
